//
//  ShebeiViewController.m
//  BlueTDevice
//
//  Created by MingmingSun on 16/9/9.
//  Copyright © 2016年 Sunmingming. All rights reserved.
//

#import "ShebeiViewController.h"
#import <SVProgressHUD.h>
#import "ShebeiViewController.h"
#import "SMMNav.h"

@interface ShebeiViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) IBOutlet UITableView *myTableview;

@end

@implementation ShebeiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"搜索设备";
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.leftBarButtonItem =
    [[UIBarButtonItem alloc] initWithTitle:@"返回"
                                     style:UIBarButtonItemStyleDone
                                    target:self
                                    action:@selector(backPressed:)];
    
    self.navigationItem.rightBarButtonItem =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                                  target:self
                                                  action:@selector(scanPressed:)];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [SVProgressHUD showWithStatus:@"搜索设备中..."];
    APPALL.myBlueTDelegate = self;
    [self scanPressed:nil];
}

-(void)scanPressed:(id)sender{
    [APPALL startToScanBlueT:nil];
}

-(void)BlueTSucceed:(NSString *)aSucc{
    if([aSucc isEqualToString:@"device"]){
        [SVProgressHUD dismiss];
        [self.myTableview reloadData];
    }else if([aSucc isEqualToString:@"service"]){
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }
}

-(void)backPressed:(id)sender{
    [SVProgressHUD dismiss];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0f;
}

#pragma mark -table委托 table delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return APPALL.peripherals.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return APPALL.peripherals.count?1:0;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"已搜索到的设备列表";
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    CBPeripheral *peripheral = [APPALL.peripherals objectAtIndex:indexPath.row];
    NSDictionary *ad = [APPALL.peripheralsAD objectAtIndex:indexPath.row];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.imageView.image = [UIImage imageNamed:@"bluetooth"];
    cell.textLabel.text = peripheral.name;
    //找到cell并修改detaisText
    /*
    NSArray *serviceUUIDs = [ad objectForKey:@"kCBAdvDataServiceUUIDs"];
    if (serviceUUIDs) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%lu个service",(unsigned long)serviceUUIDs.count];
    }else{
        cell.detailTextLabel.text = [NSString stringWithFormat:@"0个service"];
    }
    */
    //次线程读取RSSI和服务数量
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [APPALL loadDevice:indexPath.row];
    //    baby.connectToPeripheral(self.currPeripheral).begin();
    /*
    [self dismissViewControllerAnimated:YES completion:^{
        [APPALL stopScanBlueT:nil];
    }];
     */
}


/*

-(void)insertRowToTableView:(CBService *)service{
    
    
}

#pragma mark -Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.services.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    PeripheralInfo *info = [self.services objectAtIndex:section];
    return [info.characteristics count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CBCharacteristic *characteristic = [[[self.services objectAtIndex:indexPath.section] characteristics]objectAtIndex:indexPath.row];
    NSString *cellIdentifier = @"characteristicCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",characteristic.UUID];
    cell.detailTextLabel.text = characteristic.description;
    return cell;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    PeripheralInfo *info = [self.services objectAtIndex:section];
    title.text = [NSString stringWithFormat:@"%@", info.serviceUUID];
    [title setTextColor:[UIColor whiteColor]];
    [title setBackgroundColor:[UIColor darkGrayColor]];
    return title;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50.0f;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CharacteristicViewController *vc = [[CharacteristicViewController alloc]init];
    vc.currPeripheral = self.currPeripheral;
    vc.characteristic =[[[self.services objectAtIndex:indexPath.section] characteristics]objectAtIndex:indexPath.row];
    vc->baby = baby;
    [self.navigationController pushViewController:vc animated:YES];
}

*/

@end
